home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: Q:order of evaluation
- Date: 23 Jan 1996 21:02:11 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Message-ID: <4e3icj$q6o@colossus.holonet.net>
- References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <4e2f04$bnp@vixen.cso.uiuc.edu>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Scott J. McCaughrin (sjmccaug@prairienet.org) wrote:
-
- : In a previous article, 100336.3326@CompuServe.COM (Holger Maier) says:
- [...]
- : > int i=1;int j=i+(i+=1);
- [...]
- : Hi, Holger:
-
- : No, it appears that the compiler is correct.
-
- I agree, it's correct. Any behavior is acceptable for an undefined
- expression.
-
- : The parentheses override
- : any other precedence rules, so (i+=1) is executed first to set i == 2,
- : then the assignment has the effect of assigning i' + 2 (= 2 + 2) to j.
-
- No. I recommend the comp.lang.c FAQ, if you cannot see why your
- reasoning is wrong; there, the expression a[i] = i++ is analyzed
- and debunked. Note that [] has the highest precedence of any of
- the operators in that expression, but still, the value of the index
- i is undefined. The standard explicitly says so.
-
- (BTW, it is imprecise to say that parentheses override precedence
- rules. Rather, they simply *identify* their contents as an
- expression, whose value is to be used as operand for whatever
- operators are to its left and right. The parentheses themselves
- are not an operator in this syntax, so they do not in themselves
- have or imply any precedence.)
-
- Nowhere in the standard will you find anything about parenthesized
- expressions getting evaluated first. You totally made that up!
- For well-defined expressions without side effects, your assumption
- does no harm because it gives the right answer. The harm comes
- when you try to apply your assumption to undefined expressions,
- or to situations like the following:
-
- int one() { cout<<"one"; return 1; }
- int two() { cout<<"two"; return 2; }
-
- int foo = (one() + 1) * two();
-
- Prints "twoone" on my machine, even though by your reasoning the
- one() call should have come first. Your reasoning is wrong.
-
- It can be a rude awakening, when the scales fall from your eyes.
- I remember when it happened to me. Don't worry, the world
- doesn't come to an end.
- --
- Russell Blackadar, russell@mdli.com
-